﻿// ----------------------------------------------------------------------------------
// Sample Code from Fernando Machado Píriz's blog: http://fernandomachadopiriz.com
//
// <copyright company="Fernando Machado Píriz"  file="SingleInstanceManager.cs">
// Copyright (c) Fernando Machado Píriz. Distributed under FSF's GPL v2.</copyright>
// ----------------------------------------------------------------------------------
// This code and information are provided "as is" without warranty of any kind,
// either expressed of implied, including but not limited to the implied warranties
// of merchantability and/or fitness for a particular purpose.
// ----------------------------------------------------------------------------------
namespace GivingAPresentation
{
    using System;
    using System.Linq;
    using GivingAPresentation.Commons;
    using Microsoft.VisualBasic.ApplicationServices;

    /// <summary>
    /// A helper class for the logic of an application of which only one instance is allowed. The first instance runs normally but remaining
    /// instances just activates the first instance and then stop.
    /// </summary>
    public sealed class SingleInstanceManager : WindowsFormsApplicationBase
    {
        /// <summary>
        /// Prevents a default instance of the SingleInstanceManager class from being created from outside its own. Initializes a new instance
        /// of the SingleInstanceManager class. Configures the application to allow just one instance.
        /// </summary>
        private SingleInstanceManager()
        {
            IsSingleInstance = true;
        }

        /// <summary>
        /// Gets the <code>SingleInstanceApplication</code> application instance created by this helper.
        /// </summary>
        public SingleInstanceApplication Application
        {
            get;
            private set;
        }

        /// <summary>
        /// The application entry point. Creates and runs an instance of <code>SingleInstanceManger</code>.
        /// </summary>
        /// <param name="args">Contains the command-line arguments used to call this application.</param>
        [STAThread]
        public static void Main(string[] args)
        {
            SingleInstanceManager manager = new SingleInstanceManager();
            manager.Run(args);
        }

        /// <summary>
        /// Called when first application instance starts. Creates the <code>SingleInstanceApplication</code> application instances helped by
        /// this class.
        /// </summary>
        /// <param name="e">The parameter is not used.</param>
        /// <returns>Allways returns false to indicate no further startup required.</returns>
        protected override bool OnStartup(StartupEventArgs e)
        {
            this.Application = new SingleInstanceApplication();
            this.Application.Run();
            return false;
        }

        /// <summary>
        /// Called when another instance of the application starts. Passes command-line arguments to the first instance for processing.
        /// </summary>
        /// <param name="e">Contains  the command-line arguments used to call the application again.</param>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "0",
            Justification = "The call to ExceptionHelper.CheckArgumentNotNull is validating the argument")]
        protected override void OnStartupNextInstance(StartupNextInstanceEventArgs e)
        {
            ExceptionHelper.CheckArgumentNotNull(e, "StartupNextInstanceEventArgs");

            base.OnStartupNextInstance(e);
            this.Application.ProcessArgs(e.CommandLine.ToArray(), false);
        }
    }
}
